<?php
// 本类由系统自动生成，仅供测试用途
class ProviderAction extends CommonAction {
	
	/**
	 * 添加过滤条件
	 *
	 * @param
	 *        	$map
	 */
	public function _filter(&$map) {
		$providername = $_REQUEST ['providername'];
		// 不显示金海库南曹库东明库
		$map ['id'] = array (
				'not in ',
				'1,2,3' 
		);
		
		if (! empty ( $providername )) {
			$map ['providername'] = array (
					'like',
					"%" . $providername . "%" 
			);
		}
		$this->assign ( 'providername', $providername );
	}
	public function _before_index() {
		$model = M ( "Provider" );
		$list = $model->getField ( 'id,providername,contactor,phone,fax,site,email,address,zip,openbank,
				account,notes,rank' );
		$this->assign ( 'groupList', $list );
	}
	public function read() {
		$id = $_REQUEST ['id'];
		$model = M ( "Provider" );
		$Provider = $model->where ( 'id=' . $id )->find ();
		$this->assign ( 'vo', $Provider );
		$this->display ();
	}
	public function _before_add() {
		$model = M ( "Provider" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function _before_insert() {
		$model = M ( 'Provider' );
		$Provider = $model->getByProvidername ( $_POST ['providername'] );
		if ($Provider) {
			$this->ajaxReturn ( '0', '该 供应商名称 已经存在!', '300' );
		}
	}
	public function _before_foreverdelete() {
		$ids = $_REQUEST ['ids'];
		$model = M ( 'ProviderPayment' );
		$model1 = M ( 'PriceEntry' );
		$Proids = explode ( ',', $ids );
		$k = count ( $Proids );
		
		for($j = 0; $j < $k; $j ++) {
			$result = $model->where ( 'providerid=' . $Proids [$j] )->delete ();
			$result1 = $model1->where ( 'providerid=' . $Proids [$j] )->delete ();
		}
	}
	public function _before_update() {
		$model = M ( 'Provider' );
		$id = $_REQUEST ['id'];
		$Provider = $model->getByProvidername ( $_POST ['providername'] );
		if ($Provider) {
			if ($Provider ['id'] !== $id) {
				$this->ajaxReturn ( '0', '该 供应商名称 已经存在!', '300' );
			}
		}
	}
	public function _before_edit() {
		$model = M ( "Provider" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	/**
	 * 查看供货信息
	 */
	public function readSupply() {
		$model = M ( "Provider" );
		$id = $_REQUEST ['id'];
		$vo = $model->where ( 'id=' . $id )->find ();
		$this->assign ( 'vo', $vo );
		$this->assign ( 'id', $id );
		$this->display ();
	}
	/**
	 * 新增供货信息
	 */
	public function supplyadd() {
		$model = M ( "Provider" );
		$id = $_REQUEST ['id'];
		// dump($id);
		$this->assign ( 'id', $id );
		$this->display ();
	}
	
	/**
	 * 修改供货信息
	 */
	public function editsupply() {
		$id = $_REQUEST ['id'];
		$map = explode ( ',', $id );
		$i = $map [1];
		$Freight = M ( 'Provider' );
		$result = $Freight->where ( 'id=' . $map [0] )->find ();
		$this->assign ( 'vo', $result );
		$this->assign ( 'i', $i );
		$this->assign ( 'company', $result ['company' . $i] );
		$this->assign ( 'wshipment', $result ['wshipment' . $i] );
		$this->display ();
	}
	/**
	 * 更新供货信息
	 */
	public function updatesupply() {
		$id = $_REQUEST ['id'];
		$i = $_REQUEST ['i'];
		$Freight = M ( 'Provider' );
		$data ['company' . $i] = $_REQUEST ['company'];
		$data ['wshipment' . $i] = $_REQUEST ['wshipment'];
		$result = $Freight->where ( 'id=' . $id )->save ( $data );
		if ($result) {
			$this->ajaxReturn ( $result, '更新成功', 200 );
		} else {
			$this->ajaxReturn ( $result, '更新失败', 300 );
		}
	}
	public function pay() {
		$provider = M ( 'Provider' );
		$providerid = $_REQUEST ['id'];
		$this->assign ( 'providerid', $providerid );
		$providername = $provider->where ( 'id=' . $providerid )->getField ( 'providername' );
		
		// //获得子公司的信息
		$this->getSubCompany ();
		$this->assign ( 'collection', $providername );
		$this->display ();
	}
	
	// 获得子公司的信息
	public function getSubCompany() {
		$Company = M ( 'Company' );
		$subcompany = $Company->select ();
		$this->assign ( 'subcompany', $subcompany );
	}
	/**
	 * 付款信息录入
	 */
	public function insertPay() {
		$model = M ( 'ProviderPayment' );
		$model1 = M ( 'Provider' );
		$id = $_REQUEST ['providerid'];
		$payer = $_REQUEST ['payer']; // 付款方
		$collection = $_REQUEST ['collection']; // 收款方
		$Provider = $model1->where ( 'id=' . $id )->find ();
		$ton = $_REQUEST ['total'] / $_REQUEST ['price'];
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 保存当前数据对象
		$list = $model->add ();
		if ($list !== false) { // 保存成功
		                       // 根据付款信息增加供货表剩余量
			if ($ton) {
				if ($payer == "普天") {
					$data ['wshipment1'] = bcadd ( $ton, $Provider ['wshipment1'], 4 );
					$i = 1;
				} else {
					if ($payer == "兰亭") {
						$data ['wshipment2'] = bcadd ( $ton, $Provider ['wshipment2'], 4 );
						$i = 2;
					} else {
						if ($payer == "天地") {
							$data ['wshipment3'] = bcadd ( $ton, $Provider ['wshipment3'], 4 );
							$i = 3;
						}
					}
				}
				$result = $model1->where ( 'id=' . $id )->save ( $data );
				if ($result) {
					$this->ajaxReturn ( 1, '新增成功!', 200 );
				} else {
					// 失败提示
					$this->ajaxReturn ( 1, '新增失败!', 300 );
				}
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			} else {
				$this->ajaxReturn ( 1, '付款金额或单价为空!', 300 );
			}
		} else {
			// 失败提示
			$this->ajaxReturn ( 1, '新增失败!', 300 );
		}
	}
	/**
	 * 查看付款信息
	 */
	public function readPay() {
		$providerid = $_REQUEST ['id'];
		$model = M ( 'ProviderPayment' );
		$name = $model->query ( 'select providername from mlt_provider where mlt_provider.id=' . $providerid );
		import ( '@.Org.Page' );
		$count = $model->where ( 'providerid=' . $providerid )->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where ( 'providerid=' . $providerid )->page ( $pageNum . "," . $page->listRows )->order ( 'id desc' )->select ();
		
		$this->assign ( 'providername', $name [0] ['providername'] );
		// $list=$model->where('providerid='.$providerid)->select();
		$this->assign ( 'id', $providerid );
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display ();
	}
	/**
	 * 清空
	 */
	public function deleteall() {
		$id = $_REQUEST ['id'];
		$model = M ( 'ProviderPayment' );
		$result = $model->where ( 'providerid=' . $id )->delete ();
		if ($result) {
			$this->ajaxReturn ( 1, '清空成功!', 200 );
		} else {
			$this->ajaxReturn ( 1, '清空失败,请联系管理员!', 300 );
		}
	}
	
	/**
	 * 删除付款信息
	 */
	public function deletePay() {
		$pid = $_REQUEST ['pid'];
		$id = $_REQUEST ['id'];
		$model = M ( 'ProviderPayment' );
		$model1 = M ( 'Provider' );
		// 开起事务
		$model->startTrans ();
		$list = $model->find ( $pid );
		$Provider = $model1->where ( 'id=' . $id )->find ();
		if ($list) {
			// 付款金额
			$total = $list ['total'];
			// 付款公司名称
			$payer = $list ['payer'];
			// 单价
			$price = $list ['price'];
			// 计算吨数
			$ton = $total / $price;
			if ($payer == "普天") {
				$data ['wshipment1'] = bcsub ( $Provider ['wshipment1'], $ton, 4 );
				$i = 1;
			} else {
				if ($payer == "兰亭") {
					$data ['wshipment2'] = bcsub ( $Provider ['wshipment2'], $ton, 4 );
					$i = 2;
				} else {
					if ($payer == "天地") {
						$data ['wshipment3'] = bcsub ( $Provider ['wshipment3'], $ton, 4 );
						$i = 3;
					}
				}
			}
			$result = $model->where ( 'id=' . $pid )->delete ();
			$result1 = $model1->where ( 'id=' . $id )->save ( $data );
		}
		
		if ($result && $result1) {
			$model->commit ();
			$this->ajaxReturn ( 1, '删除成功!', 200 );
		} else {
			$model->rollback ();
			$this->ajaxReturn ( 1, '删除失败,请联系管理员!', 300 );
		}
	}
	
	// 双击查看付款单条记录
	public function viewaPay() {
		$id = $_REQUEST ['id'];
		$model = M ( 'ProviderPayment' );
		$vo = $model->where ( 'id=' . $id )->find ();
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	// 价格录入
	public function priceentry() {
		$model = M ( 'PriceEntry' );
		$id = $_REQUEST ['id'];
		import ( '@.Org.Page' );
		$count = $model->where ( 'providerid=\'' . $id . '\'' )->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 20 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where ( 'providerid=\'' . $id . '\'' )->page ( $pageNum . "," . $page->listRows )->order ( 'id desc' )->select ();
		$this->assign ( 'id', $id );
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display ();
	}
	// 新增价格区间
	public function priceentryadd() {
		$model = M ( "PriceEntry" );
		$id = $_REQUEST ['id'];
		$this->assign ( 'id', $id );
		$this->display ();
	}
	public function editpriceentry() {
		$id = $_REQUEST ['id'];
		$Freight = M ( 'PriceEntry' );
		$result = $Freight->where ( 'id=' . $id )->find ();
		$this->assign ( 'vo', $result );
		$this->display ();
	}
	// 更新
	public function updatepriceentry() {
		$id = $_REQUEST ['id'];
		$PriceEntry = M ( 'PriceEntry' );
		if ($PriceEntry->create ()) {
			$result = $PriceEntry->where ( 'id=' . $id )->save ();
			if ($result) {
				$this->ajaxReturn ( $result, '更新成功', 200 );
			} else {
				$this->ajaxReturn ( $result, '更新失败', 300 );
			}
		} else {
			$this->error ( $PriceEntry->getError () );
		}
	}
	public function priceentrydelete() {
		$id = $_REQUEST ['ids'];
		
		$Freight = M ( 'PriceEntry' );
		$condition = array (
				id => array (
						'in',
						explode ( ',', $id ) 
				) 
		);
		$result = $Freight->where ( $condition )->delete ();
		if ($result) {
			$this->ajaxReturn ( $result, '删除成功', 200 );
		} else {
			$this->ajaxReturn ( $result, '删除失败', 300 );
		}
	}
	
	// 价格录入
	public function insertpriceentry() {
		$model = M ( 'PriceEntry' );
		$providerid = $_REQUEST ['providerid'];
		$price = $_REQUEST ['price'];
		$data ['providerid'] = ( int ) $providerid;
		$data ['start'] = $_REQUEST ['start'];
		$data ['end'] = $_REQUEST ['end'];
		$data ['price'] = $price;
		// 自动添加截止日期
		$endtime = $_REQUEST ['start'];
		$time = strtotime ( $endtime ) - 1 * 24 * 3600;
		$maxid = $model->where ( 'providerid=' . $providerid )->max ( 'id' );
		if ($maxid != null) {
			$priceentry = $model->where ( 'id=' . $maxid )->find ();
			if ($priceentry ['end'] == 0) {
				$model->where ( 'id=' . $maxid )->setField ( 'end', date ( 'y-m-d', $time ) );
			}
		}
		$result = $model->add ( $data );
		if ($result) {
			$this->ajaxReturn ( 1, '新增成功!', 200 );
		} else {
			// 失败提示
			$this->ajaxReturn ( 1, '新增失败!', 300 );
		}
	}
	// 根据时间段，查询符合条件的调度记录信息，计算总额
	public function priceentrypay() {
		$start = $_REQUEST ['start'];
		$end = $_REQUEST ['end'];
		$stringtime = date ( 'Y-m-d h:i:s', time () );
		$id = $_REQUEST ['ids'];
		$model = M ( 'PriceEntry' );
		$model1 = M ( 'ControlTable' );
		if ($start !== null) {
			// 如果客户“至”没有输入，则默认为系统当前时间
			if ($end == null) {
				$end = $stringtime;
			}
			$providerid = $_REQUEST ['ids'];
			$ControlTable = $model1->where ( 'controldate>=\'' . $start . '\' and controldate<=\'' . $end . '\' and provider=' . $providerid )->select ();
			// 查询结果放入数组P
			$p = $model->where ( 'providerid=' . $id )->select ();
			// 改变数组P尾‘end’值为当前时间
			if ('0000-00-00 00:00:00' == $p [count ( $p ) - 1] ['end']) {
				$p [count ( $p ) - 1] ['end'] = $stringtime;
			}
			// 合计
			$ze = 0;
			$zz = 0;
			if ($ControlTable) {
				for($i = 0; $i < count ( $ControlTable ); $i ++) {
					// dump('第'.$i.'个调度'.$ControlTable [$i] ['controldate']);
					$str = substr ( $ControlTable [$i] ['controldate'], 0, 10 );
					$strtime = strtotime ( $ControlTable [$i] ['controldate'] ); // 调度时间转换为时间戳
					                                                             // $provider
					                                                             // =
					                                                             // $model->where
					                                                             // (
					                                                             // 'start<=\''
					                                                             // .$str.
					                                                             // '\' and
					                                                             // end>=\''
					                                                             // .
					                                                             // $str
					                                                             // . '\' and
					                                                             // providerid='
					                                                             // .
					                                                             // $providerid)->field
					                                                             // (
					                                                             // 'price'
					                                                             // )->select
					                                                             // ();
					for($j = 0; $j < count ( $p ); $j ++) {
						// dump('第'.$j.'个'.$p[$j]['start']);
						// dump('第'.$j.'个'.$p[$j]['end']);
						// dump($strtime);
						// dump(strtotime($p[$j]['start']).'--'.strtotime($p[$j]['end']));
						// dump('------');
						if (strtotime ( $p [$j] ['start'] ) <= $strtime) {
							if ($strtime <= strtotime ( $p [$j] ['end'] )) {
								$data [$i] ['orders'] = $ControlTable [$i] ['orders'];
								$data [$i] ['controldate'] = $ControlTable [$i] ['controldate'];
								$data [$i] ['Factorynetweight'] = $ControlTable [$i] ['Factorynetweight'];
								$data [$i] ['price'] = $p [$j] ['price'];
								$data [$i] ['rental'] = $p [$j] ['price'] * $ControlTable [$i] ['Factorynetweight'];
								$ze += $data [$i] ['rental']; // 页面初始合计显示总额
								$zz += $ControlTable [$i] ['Factorynetweight']; // 页面初始合计显示总重
							}
						}
					}
				}
				if (count ( $data ) == 0) {
					$this->ajaxReturn ( 0, '无相关记录，请确认你的价格区间！', 300 );
				}
			} else {
				$this->ajaxReturn ( 0, '无记录!请确定该供应商的价格表与调度信息', 300 );
			}
			$this->assign ( 'list', $data );
		}
		$this->assign ( 'ze', $ze );
		$this->assign ( 'count', count ( $ControlTable ) );
		$this->assign ( 'zz', $zz );
		$this->assign ( 'ids', $id );
		$this->display ();
	}
}